package com.xiaoxiang.text.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxiang.common.result.HttpResult;
import com.xiaoxiang.text.entity.Comment;
import com.xiaoxiang.text.service.CommentService;
import com.xiaoxiang.text.service.DynamicService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Comment)表控制层
 *
 * @author haha
 * @since 2022-05-01 22:56:30
 */
@RestController
@RequestMapping("comment")
public class CommentController {
    /**
     * 服务对象
     */
    @Resource
    private CommentService commentService;


    @Resource
    private DynamicService dynamicService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param comment 查询实体
     * @return 所有数据
     */
    @GetMapping("list")
    public HttpResult selectAll(Page<Comment> page, Comment comment) {
        return HttpResult.success(this.commentService.page(page, new QueryWrapper<>(comment)));
    }

    /**
     * 通过动态主键查询单条数据
     *
     * @return 单条数据
     */
    @GetMapping
    public HttpResult selectOne(@RequestParam Long dynamicId) {
        return HttpResult.success(this.commentService.getByDynamicId(dynamicId));
    }

    /**
     * 新增数据
     *
     * @param comment 实体对象
     * @return 新增结果
     */
    @PostMapping
    public HttpResult insert(@RequestBody Comment comment) {
        if (comment.getRootId() == null) {
            dynamicService.comment(comment.getDynamicId(), comment.getUserId());
        }
        return HttpResult.success(this.commentService.save(comment));
    }

    /**
     * 修改数据
     *
     * @param comment 实体对象
     * @return 修改结果
     */
    @PutMapping
    public HttpResult update(@RequestBody Comment comment) {
        return HttpResult.success(this.commentService.updateById(comment));
    }

    /**
     * 删除数据
     *
     * @return 删除结果
     */
    @DeleteMapping
    public HttpResult delete(@RequestParam("id") Long id) {
        return HttpResult.success(this.commentService.removeById(id));
    }
}
